﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.UI.HtmlControls;

namespace NJL.WebManage.SystemNews
{
    public partial class AddSystemNews : BasePage
    {
        NJL.BLL.NJL_SYS_CS_NEWS_TYPE bllNewsType = new BLL.NJL_SYS_CS_NEWS_TYPE();
        NJL.BLL.NJL_SYS_NEWS bllSysNews = new BLL.NJL_SYS_NEWS();
        NJL.Model.NJL_SYS_NEWS modelSysNews = new Model.NJL_SYS_NEWS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet ds = bllNewsType.GetList("");
                dllNewsType.DataSource = ds;
                dllNewsType.DataTextField = "TYPE_NAME";
                dllNewsType.DataValueField = "TYPE_CODE";
                dllNewsType.DataBind();
                dllNewsType.Items.Insert(0, new ListItem("请选择", "0"));
                ViewState["submitstate"] = "1";
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            modelSysNews.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
            modelSysNews.NEWS_TITLE = txtTitle.Text.Trim();
            modelSysNews.NEWS_SUBJECT_TITLE = txtSubtitle.Text.Trim();
            modelSysNews.TYPE_CODE = dllNewsType.SelectedValue;
            modelSysNews.NEWS_TAG = txtTag.Text.Trim();
            modelSysNews.IS_LINK_TO = Convert.ToInt32(rblIsTurn.SelectedValue); //1转向 0 不转向
            modelSysNews.LINK_TO_URL = txtTurnUrl.Text.Trim();
            modelSysNews.NEWS_CONTECT = txtContent.Text.Trim();
            modelSysNews.SEO_TITLE = txtSeoTitle.Text.Trim();
            modelSysNews.SEO_KEYWORDS = txtSeoKeyworkds.Text.Trim();
            modelSysNews.SEO_DESCRIPTION = txtSeoDescription.Text.Trim();
            modelSysNews.NJL_SYS_BASE_ID = WEBSITE_CODE;
            modelSysNews.CREATE_DATE_TIME = DateTime.Now;
            modelSysNews.CREATE_USER_NAME = USER_NAME;
            modelSysNews.CREATE_USER_ID = USER_ID;
            modelSysNews.UPDATE_DATE_TIME = DateTime.Now;
            modelSysNews.UPDATE_USER_NAME = USER_NAME;
            modelSysNews.UPDATE_USER_ID = USER_ID;
            modelSysNews.PUBLISH_DATE_TIME = DateTime.Now;
            modelSysNews.IS_INDEX = 0;
            modelSysNews.IS_TOP = 0;
            modelSysNews.IS_PUBLISH = 0;
            modelSysNews.SORT = int.Parse(txtSort.Text.Trim());
            if (FileUpload1.HasFile)
            {
                modelSysNews.IMAGE_CODE = SaveUploadFile(Request.Files);
            }
           if (bllSysNews.Add(modelSysNews))
            {
                DataSet ds = bllSysNews.GetList(1, "NJL_SYS_BASE_ID=" + WEBSITE_CODE, "CREATE_DATE_TIME desc");
                string id = ds.Tables[0].Rows[0][0].ToString();
                Hiddenfield1.Value = id;
                Royalsoft.Common.MessageBox.Show(this, "添加成功");
                #region 标签处理
                NJL.BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
                NJL.Model.NJL_CS_TAG modelTag = new Model.NJL_CS_TAG();
                char[] tagtxt1 = { ',', '，' };
                string[] tag = txtTag.Text.Trim().Split(tagtxt1);
                foreach (string strtag in tag)
                {
                    ds = bllTag.GetList("TAG='" + strtag + "' and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        modelTag = bllTag.GetModel(ds.Tables[0].Rows[0][0].ToString());
                        if (modelTag != null)
                        {
                            if (modelTag.USED_COUNT != null)
                            {
                                modelTag.USED_COUNT += 1;
                                if (bllTag.Update(modelTag))
                                {
                                }
                            }
                        }
                    }
                    else
                    {
                        if (strtag != "")
                        {
                            modelTag.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random(Guid.NewGuid().GetHashCode()).Next(100, 999).ToString();
                            modelTag.IS_SYS_TAG = 0;
                            modelTag.TAG = strtag;
                            modelTag.CREATE_DATE_TIME = DateTime.Now;
                            modelTag.NJL_SYS_BASE_ID = WEBSITE_CODE;
                            modelTag.SEARCH_COUNT = 0;
                            modelTag.TAG_TYPE = "6";  //新闻6
                            modelTag.USED_COUNT = 0;
                            bllTag.Add(modelTag);
                        }
                    }
                }
                #endregion 
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //this.Count++;
            //HtmlTextArea textarea = new HtmlTextArea();
            //textarea.ID = "txt0";
            //this.PlaceHolder1.Controls.Add(textarea);
            //PlaceHolder1.Controls.Add(new LiteralControl("</br>"));
            //Literal lit = new Literal();
            //StringBuilder str = new StringBuilder();
            //str.Append("<script type='text/javascript'>");
            //str.Append("CKEDITOR.replace('" + textarea.ID + "',{");
            //str.Append("filebrowserBrowseUrl: '/ckfinder/ckfinder.html',");
            //str.Append("filebrowserImageBrowseUrl: '/ckfinder/ckfinder.html?Type=Images',");
            //str.Append("filebrowserFlashBrowseUrl: '/ckfinder/ckfinder.html?Type=Flash',");
            //str.Append("filebrowserUploadUrl: '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files',");
            //str.Append("filebrowserImageUploadUrl: '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images',");
            //str.Append("filebrowserFlashUploadUrl: '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash'}");
            //str.Append(");");
            //str.Append("</script>");
            //lit.Text = str.ToString();
            //PlaceHolder1.Controls.Add(lit);

        }
        public int Count
        {
            get
            {
                if (ViewState["Count"] == null)
                    ViewState["Count"] = 0;
                return (int)ViewState["Count"];
            }
            set { ViewState["Count"] = (int)ViewState["Count"] + 1; }
        }


    }
}